package me.jessyan.mvparms.demo.mvp.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;


import com.jess.arms.base.BaseFragment;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.platform.ai.control.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.mvparms.demo.di.component.DaggerUserComponent;
import me.jessyan.mvparms.demo.di.module.UserModule;
import me.jessyan.mvparms.demo.mvp.contract.UserContract;
import me.jessyan.mvparms.demo.mvp.model.entity.Discover;
import me.jessyan.mvparms.demo.mvp.model.entity.Social;
import me.jessyan.mvparms.demo.mvp.model.entity.User;
import me.jessyan.mvparms.demo.mvp.presenter.UserPresenter;
import me.jessyan.mvparms.demo.mvp.ui.adapter.DiscoverAdapter;
import me.jessyan.mvparms.demo.mvp.ui.adapter.SocialAdapter;
import me.jessyan.mvparms.demo.mvp.ui.view.CardConfig;
import me.jessyan.mvparms.demo.mvp.ui.view.SwipeCardCallBack;
import me.jessyan.mvparms.demo.mvp.ui.view.SwipeCardLayoutManager;


@SuppressLint("ValidFragment")
public class SocialFragment extends BaseFragment<UserPresenter> implements UserContract.View,SwipeRefreshLayout.OnRefreshListener, SwipeCardCallBack.SwipeCardListener {
    @BindView(R.id.tv_title)
    TextView mTitle;

    @BindView(R.id.mSwipeRefreshLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;

    @BindView(R.id.rv_discover)
    RecyclerView mDiscover;

    @BindView(R.id.rv_social)
    RecyclerView mSocial;

    @BindView(R.id.option_discover)
    Button mOptionDiscover;

    @BindView(R.id.option_social)
    Button mOptionSocial;


    private DiscoverAdapter mAdapter;
    private SocialAdapter mSocialAdapter;
    private List<Discover> mData;
    private List<Social> mSocialData;

    public static SocialFragment newInstance() {
        SocialFragment fragment = new SocialFragment();
        return fragment;
    }

    public SocialFragment() {
    }

    @Override
    public void onResume() {
        super.onResume();
        if(getUserVisibleHint()){

        }
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerUserComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .userModule(new UserModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_discover, container, false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mTitle.setText(R.string.str_title_social);
        mData = new ArrayList<Discover>();
        mSocialData = new ArrayList<Social>();
        initRecycleView();
        mData.clear();
        mData.add(new Discover("1","图片","蓝色，是一种颜色，它是红绿蓝光的三原色中的一员，在这三种原色中它的波长最短，为440～475n纳米，属于短波长。蓝色是永恒的象征，它的种类很繁多，每一种蓝色又代表着不同的政治或其他含义，另外以蓝色命名的音乐、书籍、明星也不乏其例","",R.color.app_color));
        mData.add(new Discover("2","文章","比白色深些，比黑色浅些，比银色暗淡，比红色冷寂，穿插于黑白两色之间，更有些暗抑的美，幽幽的，淡淡的，不比黑和白的纯粹，却也不似黑和白的单一，似混沌，天地初开最中间的灰，不用和白色比纯洁，不用和黑色比空洞，而是有点单纯，有点寂寞，有点空灵，捉摸不定的，奔跑于黑白之间，像极了人心，是常变的，善变的，却是最像人的颜色。","",R.color.text_black));
        mData.add(new Discover("3","视频","给人轻快，充满希望和活力的感觉。黄色是由波长介于570－585纳米的光线所形成的颜色，红、绿色光混合可产生黄光。黄的互补色是蓝。但传统上画师以紫色作为黄的互补色","",R.color.yellow));
        mData.add(new Discover("4","音乐","欢聚传媒旗下歌手浅蓝四月通过“偶像众筹”线上项目发行了自己的首张EP《蓝图》，人气从YY站内蔓延到各大试听平台。","",R.color.button_background));
        mData.add(new Discover("5","应用","该剧以淞沪会战后沦为孤岛的上海为背景，讲述了身怀绝技却隐居做小会计的徐天，因帮共产党运送物资和保护恋人田丹而身不由己卷入了腥风血雨的厮杀，最终与日本人展开生死对决的故事。","",R.color.colorPrimary));
        mAdapter.notifyDataSetChanged();

        mSocialData.clear();
        mSocialData.add(new Social("1","GAI","1","2018.5.8 8:00",0,"老子吃火锅你吃火锅底料!","",5,0,3));
        mSocialData.add(new Social("2","大笑","2","2018.5.8 10:00",0,"I am全国捣蛋王国选出的leader~","",2,0,3));
        mSocialAdapter.notifyDataSetChanged();

        hideLoading();
    }


    private void initRecycleView() {
        ArmsUtils.configRecycleView(mDiscover,new SwipeCardLayoutManager(getActivity()));
        ArmsUtils.configRecycleView(mSocial,new LinearLayoutManager(getActivity()));
        mAdapter = new DiscoverAdapter(mData);
        mDiscover.setAdapter(mAdapter);

        CardConfig.initConfig(this);
        ItemTouchHelper.Callback callback = new SwipeCardCallBack(mData,mAdapter,mDiscover,this);
        ItemTouchHelper helper = new ItemTouchHelper(callback);
        helper.attachToRecyclerView(mDiscover);

        mSocialAdapter = new SocialAdapter(mSocialData);
        mSocial.setAdapter(mSocialAdapter);

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setProgressViewOffset(false, 0, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
                        .getDisplayMetrics()));

    }


    /**
     * 此方法是让外部调用使fragment做一些操作的,比如说外部的activity想让fragment对象执行一些方法,
     * 建议在有多个需要让外界调用的方法时,统一传Message,通过what字段,来区分不同的方法,在setData
     * 方法中就可以switch做不同的操作,这样就可以用统一的入口方法做不同的事
     * <p>
     * 使用此方法时请注意调用时fragment的生命周期,如果调用此setData方法时onCreate还没执行
     * setData里却调用了presenter的方法时,是会报空的,因为dagger注入是在onCreated方法中执行的,然后才创建的presenter
     * 如果要做一些初始化操作,可以不必让外部调setData,在initData中初始化就可以了
     *
     * @param data
     */

    @Override
    public void setData(Object data) {

    }


    @Override
    public void showLoading() {

    }



    @Override
    public void hideLoading() {
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void showMessage(@NonNull String message) {
        if (!message.isEmpty()) {

        }
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {

    }

    @Override
    public void killMyself() {

    }


    @Override
    public void setAdapter(DefaultAdapter adapter) {

    }

    @Override
    public void loginReturn(User user) {

    }

    @Override
    public void logout() {

    }

    public void uploadResult(String string) {

    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        Discover remove = mData.remove(viewHolder.getLayoutPosition());
        mData.add(0, remove);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        //监听话滑动的距离--控制动画的执行程度
        //灵界点
        double maxDistance = recyclerView.getWidth() * 0.5f;
        double distance = Math.sqrt(dX * dX);
        //动画执行的百分比
        double fraction = distance / maxDistance;
        if (fraction > 1) {
            fraction = 1;
        }
        int itemcount = recyclerView.getChildCount();
        for (int i = 0; i < itemcount; i++) {
            //执行
            View view = recyclerView.getChildAt(i);
            //几个view层叠的效果，错开的效果--便宜动画+缩放动画
            int level = itemcount - i - 1;
            if (level > 0) {
                if (level < CardConfig.MAX_SHOW_COUNT- 1) {
                    view.setTranslationY((float)(1-CardConfig.TRANS_V_GAP*level+fraction*CardConfig.TRANS_V_GAP));
                    view.setScaleX((float)(1-CardConfig.SCALE_GAP*level+fraction*CardConfig.SCALE_GAP));
                    view.setTranslationY((float)(1-CardConfig.SCALE_GAP*level+fraction*CardConfig.SCALE_GAP));
                }
            }


        }
    }

    @OnClick(R.id.option_discover)
    public void cutDiscover(View view){
        mOptionDiscover.setBackgroundResource(R.color.app_color);
        mOptionDiscover.setTextColor(Color.parseColor("#ffffff"));
        mOptionSocial.setBackgroundResource(R.color.TianChong);
        mOptionSocial.setTextColor(Color.parseColor("#333333"));
        mDiscover.setVisibility(View.VISIBLE);
        mSocial.setVisibility(View.GONE);

    }

    @OnClick(R.id.option_social)
    public void cutSocial(View view){
        mOptionSocial.setBackgroundResource(R.color.app_color);
        mOptionSocial.setTextColor(Color.parseColor("#ffffff"));
        mOptionDiscover.setBackgroundResource(R.color.TianChong);
        mOptionDiscover.setTextColor(Color.parseColor("#333333"));
        mDiscover.setVisibility(View.GONE);
        mSocial.setVisibility(View.VISIBLE);
    }
}
